$(document).ready(function (){
    //获取用户信息
    let user = sessionStorage.getItem('user');
    user = JSON.parse(user);

    $("#selectAll").on('change',function (){
        if ($(this).prop('checked')){
            $('.join-settlement').prop('checked',true).trigger('change')
        }else {
            $('.join-settlement').prop('checked',false).trigger('change')
        }
    })

    let vm1 = new Vue({
        el:'#goodImages',
        data:{
            imageList:[]
        },
        methods:{
            aa(ele){
                $(ele).find('.remove').css('display','inline')
                $(ele).css('cursor','pointer')
            },
            bb(ele){
                $(ele).find('.remove').css('display','none')
            },
            deselect(ele,index,idx){
                vm1.imageList.splice(index,1)
                if (vm1.imageList.length <= 0){
                    $(".item-name").css('display','inline-block');
                    $("#goodImages").css('display','none');
                    $('#settlement').prop('disabled',true)
                }
                $(ele).closest('.cart-right').prev('.cart-left').find('.cart-item').eq(idx).find('.join-settlement').prop('checked',false)
                $(ele).closest('.cart-right').prev('.cart-left').find('.cart-item').eq(idx).css('backgroundColor','')
                let num = $("#number").text()
                num --;
                $("#number").text(num)
                let  amount = parseFloat($('#amount').text());
                console.log(amount)
                let singlePrice = $(ele).closest('.cart-right').prev('.cart-left').find('.cart-item').eq(idx).find('.price').text();
                let number = $(ele).closest('.cart-right').prev('.cart-left').find('.cart-item').eq(idx).find('.number').text();
                amount =  amount - singlePrice*number
                console.log(singlePrice)
                $('#amount').text( amount.toFixed(2));
                $(ele).closest('.cart-right').prev('.cart-left').find('.cart-item').eq(idx).find('.number').text('1');
            }
        }
    })

    let vm2 = new Vue({
        el:'#goods',
        data:{
            goodList:[]
        },
        methods:{
            add(ele){
                $(ele).prevAll('.cut').attr('disabled',false)
                let number = $(ele).prevAll('.number').text();
                number ++;
                $(ele).prevAll('.number').text(number)
                if ($(ele).closest('.item-quantity').prevAll('.join-settlement').prop('checked')){
                    let  amount = parseFloat($('#amount').text());
                    let singlePrice = parseFloat($(ele).closest('.item-quantity').prevAll('.item-info').find('.price').text());
                    amount =  amount + singlePrice
                    $('#amount').text( amount.toFixed(2));
                }
            },
            cut(ele){
                let number = $(ele).next('.number').text();
                if (number <= 1){
                    $(ele).attr('disabled',true)
                    return
                }
                number --;
                $(ele).next('.number').text(number)
                if ($(ele).closest('.item-quantity').prevAll('.join-settlement').prop('checked')){
                    let  amount = parseFloat($('#amount').text());
                    let singlePrice = parseFloat($(ele).closest('.item-quantity').prevAll('.item-info').find('.price').text());
                    amount =  amount - singlePrice
                    $('#amount').text( amount.toFixed(2));
                }
            },
            joinSettlement(ele,index){
                if ($(ele).prop('checked')){
                    $('#settlement').prop('disabled',false)
                    $(".item-name").css('display','none');
                    $("#goodImages").css('display','inline-block');
                    $(ele).closest('.cart-item').css('backgroundColor','#FFF6F2')
                    let num = $("#number").text()
                    num ++ ;
                    $("#number").text(num)
                    vm1.imageList.push({pic:$(ele).next('.item-info').find(".item-image").prop('src'),idx:index})
                    let  amount = parseFloat($('#amount').text());
                    let singlePrice = $(ele).next('.item-info').find('.price').text();
                    let number = $(ele).nextAll('.item-quantity').find('.number').text();
                    amount =  amount + singlePrice*number
                    $('#amount').text( amount.toFixed(2));
                }else {
                    vm1.imageList.splice(index,1)
                    if (vm1.imageList.length <= 0){
                        $(".item-name").css('display','inline-block');
                        $("#goodImages").css('display','none');
                        $('#settlement').prop('disabled',true)
                    }
                    $(ele).closest('.cart-item').css('backgroundColor','')
                    let num = $("#number").text()
                    num --;
                    $("#number").text(num)
                    let  amount = parseFloat($('#amount').text());
                    let singlePrice = $(ele).next('.item-info').find('.price').text();
                    let number = $(ele).nextAll('.item-quantity').find('.number').text();
                    amount =  amount - singlePrice*number
                    $('#amount').text( amount.toFixed(2));
                }
            },
            del(ele,index,id){
                $.post('shoppingCart/del_shoppingCart_byGoodId',{goodId:id},function (){
                    vm1.imageList.splice(index,1)
                    if (vm1.imageList.length <= 0){
                        $(".item-name").css('display','inline-block');
                        $("#goodImages").css('display','none');
                        $('#settlement').prop('disabled',true)
                    }
                    let num = $("#number").text()
                    num --;
                    $("#number").text(num)
                    let  amount = parseFloat($('#amount').text());
                    let singlePrice = $(ele).closest('.item-actions1').prevAll('.item-info').find('.price').text();
                    let number = $(ele).closest('.item-actions1').prev('.item-quantity').find('.number').text();
                    amount =  amount - singlePrice*number
                    $('#amount').text( amount.toFixed(2));
                    vm2.goodList.splice(index,1)
                    $.post('shoppingCart/get_shoppingCartTotal',{ userId:user.userId},function (total){
                        $('#total span').text(total);
                        $('#total2').text(total)
                    })
                })
            }
        },
        beforeCreate:function (){
            $.post('shoppingCart/get_shoppingCart_byUserId',{userId:user.userId},function (res){
                vm2.goodList = res.data;
            })
        }
    })
})